package com.wuda.servlet.user;

import com.wuda.bean.BorrowRecord;
import com.wuda.dao.BorrowRecordDao;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/user/getBorrowHistory")
public class GetBorrowHistory extends HttpServlet {

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("application/json; charset=utf8");

		int userId = Integer.parseInt(req.getParameter("userId"));


		BorrowRecordDao borrowRecordDao = new BorrowRecordDao();
		List<BorrowRecord> borrowHistory = borrowRecordDao.getBorrowHistory(userId, false); // 未归还的借书历史

		JSONArray jsonArray = BorrowRecordDao.getJsonArray(borrowHistory);

		JSONObject jsonResult = new JSONObject();
		jsonResult.put("code", 200);
		jsonResult.put("msg", "查询成功");
		jsonResult.put("data", jsonArray);

		try (PrintWriter out = resp.getWriter()) {
			out.print(jsonResult);
			out.flush();
		}
	}

}
